package com.petecapra.cashflow.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.petecapra.cashflow.model.FrequencyType;

public final class DateUtils {

	public static List<Date> getFrequencyDates(FrequencyType frequency, Date scheduleStart, int occurrences) {
		
		// get the calendar time
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(scheduleStart);
		
		// initialise the date array and the counter
		List<Date> frequencyDates = new ArrayList<Date>(0);
		int counter = occurrences;
		int calendarType = getCalendarType(frequency);
		int calendarAmount = getCalendarAmount(frequency);

		// get the dates
		while (counter != 0) {
			counter--;
			frequencyDates.add(calendar.getTime());
			calendar.add(calendarType, calendarAmount);			
		}
		
		// return date array
		return frequencyDates;
		
	}
	
	private static int getCalendarType(FrequencyType frequency) {
		if (frequency.equals(FrequencyType.Monthly))
			return Calendar.MONTH;
		else if (frequency.equals(FrequencyType.Quarterly))
			return Calendar.MONTH;
		else if (frequency.equals(FrequencyType.Biannually))
			return Calendar.MONTH;
		else if (frequency.equals(FrequencyType.Yearly))
			return Calendar.YEAR;			
		else
			return Calendar.DATE;
	}
	
	private static int getCalendarAmount(FrequencyType frequency) {
		if (frequency.equals(FrequencyType.Weekly))
			return 7;
		else if (frequency.equals(FrequencyType.Fortnightly))
			return 14;
		else if (frequency.equals(FrequencyType.Quarterly))
			return 3;
		else if (frequency.equals(FrequencyType.Biannually))
			return 6;
		else 
			return 1;
	}
	
}